package com.music;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class FirstLevelActivity extends Activity {
	// implements OnClickListener

	final static String TAG = "Music Application";

	private SoundManager mSoundManager;
	private MediaPlayer mMediaPlayer;
	private TextView tx;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.firstlevel);

		mSoundManager = new SoundManager();
		mSoundManager.initSounds(getBaseContext());

		mSoundManager.addSound(1, R.raw.do1);
		mSoundManager.addSound(2, R.raw.re);
		mSoundManager.addSound(3, R.raw.mi);
		mSoundManager.addSound(4, R.raw.fa);

		Button button1 = (Button) this.findViewById(R.id.Button01);
		button1.setBackgroundColor(Color.DKGRAY);
		button1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

				mSoundManager.playSound(1);

			}
		});

		Button button2 = (Button) this.findViewById(R.id.Button02);
		button2.setBackgroundColor(Color.DKGRAY);
		button2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mSoundManager.playSound(2);

			}
		});

		Button button3 = (Button) this.findViewById(R.id.Button03);
		button3.setBackgroundColor(Color.DKGRAY);
		button3.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

				mSoundManager.playSound(3);
			}
		});

		Button button4 = (Button) this.findViewById(R.id.Button04);
		button4.setBackgroundColor(Color.DKGRAY);
		button4.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

				mSoundManager.playSound(4);
			}
		});

		Button buttonPlay = (Button) this.findViewById(R.id.buttonPlay);
		buttonPlay.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				startService(new Intent(MusicService.ACTION_PLAY));
				Log.i(TAG, "My play button");
			}
		});

		// mPlayButton =(Button)findViewById(R.id.playbutton);
		// mPauseButton =(Button)findViewById(R.id.pausebutton);
		// // mRewindButton =(Button)findViewById(R.id.rewindbutton);
		// mStopButton =(Button)findViewById(R.id.stopbutton);
		//
		// mPlayButton.setOnClickListener(this);
		// mPauseButton.setOnClickListener(this);
		// mStopButton.setOnClickListener(this);

		Button buttonPause = (Button) this.findViewById(R.id.buttonPause);
		buttonPause.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				startService(new Intent(MusicService.ACTION_PAUSE));
				Log.i(TAG, "My pause button");
			}
		});

		Button buttonStop = (Button) this.findViewById(R.id.buttonStop);
		buttonStop.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				startService(new Intent(MusicService.ACTION_STOP));
				Log.i(TAG, "My stop button");
			}
		});

		playAudio();
		// System.err.println("is play working?");

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		SoundManager.cleanup();
		if (mMediaPlayer != null) {
			mMediaPlayer.release();
			mMediaPlayer = null;
		}
	}

	private void playAudio() {
		final int musicFile = R.raw.firstlevel;

		Runnable nmyRunnable = new Runnable() {

			@Override
			public void run() {

				Intent serviceIntent = new Intent(
						MusicService.ACTION_INITIALPLAY);

				serviceIntent.putExtra("musicFileName", musicFile);
				startService(serviceIntent);
				System.err.println("is run working?");
			}

		};
		Handler myHandler = new Handler();
		myHandler.postDelayed(nmyRunnable, 1000);

	}

	// private void showText(){
	//
	// Runnable nmyRunnable2 = new Runnable() {
	//
	// @Override
	// public void run() {
	// instruction.setText("Now, It is your turn!");
	//
	// }
	// };
	// Handler myHandler2 = new Handler();
	// myHandler2.postDelayed(nmyRunnable2, 3000);
	//
	// }

}